from django.shortcuts import render,HttpResponse
from django.views.decorators.csrf import csrf_exempt
from app.healthy_diet import healthy
from django.http import JsonResponse

'''
爬取菜品菜谱列表信息
'''
@csrf_exempt
def HeathlyDeit_list(request):
    if request.method == "POST":
        cid = request.POST.get("cid")
        page = request.POST.get("page")
        try:
            # 爬取菜品菜谱列表信息
            result = healthy.diet_list(cid, page)
            data = {
                "errcode": 0,
                "errmsg": "成功",
                "data": {
                    "content": result
                }
            }
        except:
            data = {
                "errcode": 10000,
                "errmsg": "失败"
            }

    return JsonResponse(data=data)


'''
文章详情
'''
@csrf_exempt
def ArticleDesc(request):
    if request.method == "POST":
        url = request.POST.get("url")

        try:
            result = healthy.HeathlyArticle(url)
            data = {
                "errcode": 0,
                "errmsg": "成功",
                "data": {
                    "content": result
                }
            }
        except:
            data = {
                "errcode": 10000,
                "errmsg": "失败"
            }
    return JsonResponse(data=data)
